<?php

namespace app\api\controller\mjlbb\coupon;
use app\api\validate\CouponValidate;
use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * NAME: 优惠劵
 * Class Coupon
 *
 * @package app\api\controller\mjlbb\coupon
 */
class Coupon extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    private $validate;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->validate = new CouponValidate();
        
    }
    
    /**
     * NAME: 可使用的优惠劵列表
     * @author Henry
     */
    public function getCouponYesList()
    {
        $param = [
            'openid' => $this->request->param('openid'),
        ];
        if(!$this->validate->check($param)){
            ajax_info(1, $this->validate->getError());
        }
        
        $coupon = Db::table('fa_shopro_coupons')
            ->field('c.*')
            ->alias('c')
            ->join('fa_shopro_user_coupons uc', 'c.id = uc.coupons_id', 'LEFT')
            ->where('c.gettimestart', '<=', time())
            ->where('c.gettimeend'  , '>=', time())
            ->where('uc.coupons_id', null)
            ->select();
        
        if(!$coupon){
            ajax_info(0, 'error', []);
        }
        
        ajax_info(1, 'success', $coupon);
    }
    
    
    
    public function getCouponNoList()
    {
        $param = [
            'openid' => $this->request->param('openid'),
        ];
        if(!$this->validate->check($param)){
            ajax_info(1, $this->validate->getError());
        }
        
        $coupon = Db::table('fa_shopro_coupons')
            ->field('c.*')
            ->alias('c')
            ->join('fa_shopro_user_coupons uc', 'c.id = uc.coupons_id', 'LEFT')
            ->where('c.usetimestart', '<=', time())
            ->where('c.usetimeend'  , '>=', time())
            ->where('uc.coupons_id', null)
            ->select();
        
        if(!$coupon){
            ajax_info(0, 'error', []);
        }
        
        ajax_info(1, 'success', $coupon);
    }
}
